package cn.itcast.bos.web.action.base;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.xml.resolver.apps.resolver;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.service.base.AreaService;
import cn.itcast.bos.utils.PinYinDuoYinUtils;
import cn.itcast.bos.web.action.common.BaseAction;

public class AreaAction extends BaseAction<Area> {
	@Resource
	private AreaService areaService;

	// 接收上传文件
	private File file;

	public void setFile(File file) {
		this.file = file;
	}

	// 文件上传
	@Action(value = "area_batchImport")
	public String areaBatchImport() throws FileNotFoundException, IOException {
		List<Area> areas = new ArrayList<>();
		// 基于.xls的代码格式解析HSSF
		// 加载EXCEL文件对象
		HSSFWorkbook hssfWorkbook = new HSSFWorkbook(new FileInputStream(file));
		// 读取一个sheet
		HSSFSheet sheetAt = hssfWorkbook.getSheetAt(0);
		// 3、 读取sheet中每一行
		for (Row row : sheetAt) {
			// 一行数据 对应 一个区域对象
			if (row.getRowNum() == 0) {
				// 第一行 跳过
				continue;
			}
			// 跳过空行
			if (row.getCell(0) == null || StringUtils.isBlank(row.getCell(0).getStringCellValue())) {
				continue;
			}
			Area area = new Area();
			area.setId(row.getCell(0).getStringCellValue());
			area.setProvince(row.getCell(1).getStringCellValue());
			area.setCity(row.getCell(2).getStringCellValue());
			area.setDistrict(row.getCell(3).getStringCellValue());
			area.setPostcode(row.getCell(4).getStringCellValue());
			// 基于pinyin4j生成城市编码和简码
			String province = area.getProvince();// 省
			String city = area.getCity();// 市
			String district = area.getDistrict();// 区
			// 去除省市区这三个后缀
			province = province.substring(0, province.length() - 1);
			city = city.substring(0, city.length() - 1);
			district = district.substring(0, district.length() - 1);
			// 编写简码
			String headArray = PinYinDuoYinUtils.convertChineseTogetHeadByString(province + city + district, true);
			area.setShortcode(headArray);
			String cityCode = PinYinDuoYinUtils.convertChineseToPinyin(city, false);
			area.setCitycode(cityCode);
			areas.add(area);
		}
		areaService.saveBatch(areas);
		return NONE;
	}

	// 有条件的分页查询
	@Action(value = "area_pageQuery", results = { @Result(name = "success", type = "json") })
	public String pageQuery() {
		// 构造分页查询条件
		Pageable pageable = new PageRequest(page - 1, rows);
		// 构造条件查询对象
		Specification<Area> specification = new Specification<Area>() {
			@Override
			public Predicate toPredicate(Root<Area> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<Predicate>();
				if (StringUtils.isNotBlank(model.getProvince())) {
					Predicate p1 = cb.like(root.get("province").as(String.class), "%" + model.getProvince() + "%");
					list.add(p1);
				}
				if (StringUtils.isNotBlank(model.getCity())) {
					Predicate p2 = cb.like(root.get("city").as(String.class), "%" + model.getCity() + "%");
					list.add(p2);
				}
				if (StringUtils.isNotBlank(model.getDistrict())) {
					Predicate p3 = cb.like(root.get("district").as(String.class), "%" + model.getDistrict() + "%");
					list.add(p3);
				}
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		//调用业务层 完成查询
		Page<Area> pageData = areaService.findPageData(specification,pageable);
		pushPageDataToValueStack(pageData);
		return SUCCESS;
	}
	
	//查询所有区域
	@Action(value = "find_area", results = { @Result(name = "success", type = "json") })
	public String findArea(){
		List<Area> list = areaService.findArea();
		ActionContext.getContext().getValueStack().push(list);
		return SUCCESS;
	}
}
